package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.TrainAuditManagerDetail;
import com.xmy.cultivate.entity.TrainAuditManagerSet;
import com.xmy.cultivate.service.ITrainAuditManagerDetailService;
import com.xmy.cultivate.service.ITrainAuditManagerSetService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.IdWorker;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 审核主管设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-11-07
 */
@RestController
@RequestMapping("/api/trainAuditManagerSet")
public class TrainAuditManagerSetController {

    @Autowired
    private ITrainAuditManagerSetService iTrainAuditManagerSetService;

    @Autowired
    private ITrainAuditManagerDetailService iTrainAuditManagerDetailService;

    @Autowired
    private IdWorker idWorker;

    @GetMapping("/page")
    public Result page(
            @RequestParam(value = "pageNum", defaultValue = "1", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10", required = false) Integer pageSize,
            @RequestParam(value = "yearPart", required = false) String yearPart,  //年份
            @RequestParam(value = "quarterNum", required = false) String quarterNum, //学期
            @RequestParam(value = "departmentId") Long departmentId,  //部门 教学部门23，市场部门27
            @RequestParam(value = "type", required = false) Integer type,// 部门为23时，1教学审核；部门为27时，1市场招生培训审核
            @RequestParam(value = "subType", required = false) Integer subType,// 教学审核时（type为1时）1：练课、2：备课
            @RequestParam(value = "name", required = false) String name
    ) {
        IPage<TrainAuditManagerSet> iPage = new Page<>(pageNum, pageSize);
        QueryWrapper<TrainAuditManagerSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("*", "getStaffNameForId(teacher_id) AS teacher_name");
        queryWrapper.eq(StringUtils.isNotEmpty(yearPart), "year_part", yearPart);
        queryWrapper.eq(StringUtils.isNotEmpty(quarterNum), "quarter_num", quarterNum);
        queryWrapper.eq("department_id", departmentId);
        queryWrapper.eq("type", type);
        queryWrapper.eq("sub_type", subType);
        queryWrapper.like(StringUtils.isNotEmpty(name), "getStaffNameForId(teacher_id)",name);
        queryWrapper.orderByDesc("created_at");
        IPage<TrainAuditManagerSet> page = iTrainAuditManagerSetService.page(iPage, queryWrapper);
        return new Result(Code.GET_OK, new PageResult<>(page.getRecords(), page.getTotal(), page.getPages()), Code.GET_OK_MSG);
    }

    @PostMapping("/save")
    public Result save(@RequestBody TrainAuditManagerSet trainAuditManagerSet) {
        long count = iTrainAuditManagerSetService.count(new QueryWrapper<>(trainAuditManagerSet));
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该年份该学期已存在该老师，无法添加");
        }
        trainAuditManagerSet.setId(idWorker.nextId());
        boolean save = iTrainAuditManagerSetService.save(trainAuditManagerSet);
        if (save) {
            return new Result(Code.OK, null, Code.SAVE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.SAVE_ERROR_MSG);

    }

    @PutMapping("/update")
    public Result update(@RequestBody TrainAuditManagerSet trainAuditManagerSet) {
        TrainAuditManagerSet trainAuditTeacherSetOld = iTrainAuditManagerSetService.getById(trainAuditManagerSet.getId());
        if (trainAuditTeacherSetOld == null) {
            return new Result(Code.UPDATE_ERROR, null, "该条数据不存在");
        }

        QueryWrapper<TrainAuditManagerSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("id", trainAuditManagerSet.getId());
        queryWrapper.eq("year_part", trainAuditTeacherSetOld.getYearPart());
        queryWrapper.eq("quarter_num", trainAuditTeacherSetOld.getQuarterNum());
        queryWrapper.eq("teacher_id", trainAuditManagerSet.getTeacherId());
        queryWrapper.eq("department_id", trainAuditTeacherSetOld.getDepartmentId());
        queryWrapper.eq("type", trainAuditTeacherSetOld.getType());
        queryWrapper.eq("sub_type", trainAuditManagerSet.getSubType());
        long count = iTrainAuditManagerSetService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.EXP_ERROR, null, "该年份该学期已存在该老师，无法添加");
        }
        boolean update = iTrainAuditManagerSetService.updateById(trainAuditManagerSet);
        if (update) {
            return new Result(Code.OK, null, Code.UPDATE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.UPDATE_ERROR_MSG);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id") String id) {

        QueryWrapper<TrainAuditManagerDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("audit_manager_set_id", id);
        long count = iTrainAuditManagerDetailService.count(queryWrapper);
        if (count > 0) {
            return new Result(Code.DELETE_ERROR, null, "该条数据有教师未删除，不可删除");
        }

        boolean delete = iTrainAuditManagerSetService.removeById(id);
        if (delete) {
            return new Result(Code.OK, null, Code.DELETE_OK_MSG);
        }
        return new Result(Code.ERROR, null, Code.DELETE_ERROR_MSG);
    }
}
